import static org.junit.Assert.*;

import java.util.List;
import java.util.Scanner;

import org.junit.*;

/**
 * @author joncaddey
 * @version 1.0
 */
public class ContinentReaderTest {

	private ContinentReader my_reader;

	@Before
	public void setup() {
		my_reader = new ContinentReader();
	}

	/**
	 * Input is empty!
	 */
	@Test(expected = IllegalArgumentException.class)
	public void testEmptyRead() {
		Scanner scanner = new Scanner("");
		my_reader.read(scanner);
	}

	/**
	 * First line is blank!
	 */
	@Test(expected = IllegalArgumentException.class)
	public void testFirstLineBlankRead() {
		Scanner scanner = new Scanner("\nContinent\n5\nTerrA\nTerrB");
		my_reader.read(scanner);
	}

	/**
	 * Continent name is whitespace!
	 */
	@Test(expected = IllegalArgumentException.class)
	public void testWhiteSpaceNameRead() {
		Scanner scanner = new Scanner(" \t \n5\nTerrA\nTerrB");
		my_reader.read(scanner);
	}

	/**
	 * Continent Value is missing!
	 */
	@Test(expected = IllegalArgumentException.class)
	public void testNoValueRead() {
		Scanner scanner = new Scanner("Continent\nTerrA\nTerrB");
		my_reader.read(scanner);
	}

	/**
	 * No Territories in Continent!
	 */
	@Test(expected = IllegalArgumentException.class)
	public void testNoTerritoriesRead() {
		Scanner scanner = new Scanner("Continent\n5\n");
		my_reader.read(scanner);
	}

	/**
	 * Can read a single continent, without blank line at end, successfully.
	 */
	@Test
	public void testSingleOkayRead() {
		Scanner scanner = new Scanner("Continent\n5\nTerrA\nTerrB");
		my_reader.read(scanner);
		assertEquals("Wrong number continents", 1, my_reader.getContinents()
				.size());
		Continent c = my_reader.getContinents().get(0);
		List<Territory> territories = c.getTerritories();
		assertEquals("wrong number territories", 2, territories.size());
		assertEquals("1st territory name wrong", "TerrA", territories.get(0)
				.getName());
		assertEquals("2nd territory name wrong", "TerrB", territories.get(1)
				.getName());

	}

	/**
	 * Can read two continents, with extra blank at end.
	 */
	@Test
	public void testDoubleOkayRead() {
		Scanner scanner = new Scanner("Continent\n5\nTerrA\nTerrB\n\n"
				+ "Continent2\n2\nTerrC\n");
		my_reader.read(scanner);
		assertEquals("Wrong number continents", 2, my_reader.getContinents()
				.size());
		Continent c = my_reader.getContinents().get(1);
		List<Territory> territories = c.getTerritories();
		assertEquals("wrong number territories", 1, territories.size());
		assertEquals("territory name wrong", "TerrC", territories.get(0)
				.getName());

	}

	/**
	 * Duplicate Continents!
	 */
	@Test(expected = IllegalArgumentException.class)
	public void testDuplicateContinentRead() {
		Scanner scanner = new Scanner("Continent\n5\nTerrA\nTerrB\n\n"
				+ "Continent\n5\nTerrA");
		my_reader.read(scanner);
	}

	/**
	 * Correct territories when same territory in multiple Continent
	 */
	@Test
	public void testDuplicateTerritoryGetTerritories() {
		Scanner scanner = new Scanner("Continent1\n5\nTerrA\nTerrB\n\n"
				+ "Continent2\n5\nTerrA");
		my_reader.read(scanner);
		assertEquals("Wrong number of territories", 2,
				my_reader.getTerritories().size());
	}

}
